package com.bhs.sansong.ui.order;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.bhs.sansong.R;
import com.bhs.sansong.adapter.OrderGoodsAdapter;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.bean.OrderGoodsBean;
import com.bhs.sansong.bean.OrderGoodsDataBean;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.ui.dialog.MyClickListener;
import com.bhs.sansong.utils.MyUtils;
import com.bhs.sansong.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 取货搜索
 */
public class ClaimGoodsActivity extends BaseActivity implements NetCallBack {

    private EditText mEtSearch;
    private RecyclerView mRvWaybill;
    private OrderGoodsAdapter orderGoodsAdapter;
    private int page = 1;

    @Override
    public int initLayout() {
        return R.layout.activity_claim_goods;
    }

    @Override
    public void initView() {
        mEtSearch = findViewById(R.id.et_search);
        mRvWaybill = findViewById(R.id.rv_waybill);
        loadData(getIntent().getStringExtra("value"));
    }

    @Override
    public void initData() {
        mEtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //点击搜索要做的操作
                    page = 1;
                    loadData(mEtSearch.getText().toString());
                    return true;
                }
                return false;
            }
        });

        OrderOperation orderOperation = new OrderOperation(this);
        orderGoodsAdapter = new OrderGoodsAdapter();
        mRvWaybill.setAdapter(orderGoodsAdapter);
        mRvWaybill.setLayoutManager(new LinearLayoutManager(mActivity));
        orderGoodsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                OrderGoodsBean data = (OrderGoodsBean) adapter.getData().get(position);
                startActivity(new Intent(mActivity, OrderDetailsActivity.class)
                        .putExtra("deliver_sn", data.getDeliver_sn()));
            }
        });

        orderOperation.operation(orderGoodsAdapter);
        orderOperation.setMyClickListener(new MyClickListener() {
            @Override
            public void onClick(View v) {
                page = 1;
                loadData(mEtSearch.getText().toString());
            }
        });
    }

    private Map<String, String> parameters = new ArrayMap<>();

    private void loadData(String search_key) {
        parameters.put("search_key", search_key);
        parameters.put("page", String.valueOf(page));
        parameters.put("search_type", String.valueOf(3));
        networkRequest(netApi.get_order_list(parameters), this);
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        OrderGoodsDataBean dataBean = new Gson().fromJson(json, OrderGoodsDataBean.class);
        if (dataBean.isStatus()) {
            if (page == 1) {
                orderGoodsAdapter.setNewData(dataBean.getData().getList());
            } else {
                orderGoodsAdapter.loadMoreComplete();
                orderGoodsAdapter.addData(dataBean.getData().getList());
            }
            if (orderGoodsAdapter.getData().size() >= dataBean.getData().getTotal_count()) {
                orderGoodsAdapter.loadMoreEnd();
            }
        } else {
            ToastUtil.show(dataBean.getMsg());
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }
}